package com.spider.iceCream.spiderComponent.parse;

import com.spider.iceCream.spiderComponent.model.VideoModel;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import javax.xml.xpath.XPathException;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//获取一个排行榜信息
public class parse {
    public static List<VideoModel> getData(String html) throws XPathException,Exception{
        List<VideoModel> dataList = new ArrayList<VideoModel>();
        HtmlCleaner cleaner = new HtmlCleaner();
        TagNode node = cleaner.clean(html);

        Object[] videos = node.evaluateXPath("//*[@class='rank-list']/li");

        Object[] oid = node.evaluateXPath("//*[@class='rank-list']/li/@data-id");
        Object[] title = node.evaluateXPath("//*[@class='rank-list']/li//div[@class='info']/a");
        Object[] playNum = node.evaluateXPath("//*[@class='rank-list']/li//div[@class='detail']/span[1]");
        Object[] danmuNum = node.evaluateXPath("//*[@class='rank-list']/li//div[@class='detail']/span[2]");
        Object[] author = node.evaluateXPath("//*[@class='rank-list']/li//div[@class='detail']/a/span");
        Object[] point = node.evaluateXPath("//*[@class='rank-list']/li//div[@class='info']//div[@class='pts']/div");
        Object[] rank = node.evaluateXPath("//*[@class='rank-list']/li/div[1]");
        Object[] videoUrl = node.evaluateXPath("//*[@class='rank-list']/li//div[@class='info']/a/@href");
        //由于无法对图片的网址进行标签定位，现对源代码进行正则处理
        ArrayList<String> imgUrl = new ArrayList<String>();
        ArrayList<String> img = new ArrayList<String>();
        Pattern p1 = Pattern.compile("\"others\".*?\"tid\"(.*?)]");
        // get a matcher object
        Matcher m1 = p1.matcher(html);
        String INPUT = m1.replaceAll("");
        //System.out.println(INPUT);
        Pattern p2 = Pattern.compile("\"copyright\":.*?,\"pic\":\"(.*?)\",\"title\":\".*?\"");
        Matcher m2 = p2.matcher(INPUT);
        while (m2.find()){
            imgUrl.add(m2.group(1));
        }
        Pattern p3 = Pattern.compile("u002F");
        for (String s:
                imgUrl    ) {
            Matcher m3 = p3.matcher(s);
            String input = m3.replaceAll("");
            img.add(input);
        }
        String[] url = new String[videoUrl.length];
        for (int i = 0;i<videos.length;i++){
            VideoModel model = new VideoModel();

            url[i] = videoUrl[i].toString();
            String[] url1 = url[i].split("//");
            String url2 = url1[1];
            int intOid = Integer.parseInt(oid[i].toString());
            int intRank = Integer.parseInt(((TagNode)rank[i]).getText().toString());
            String upper=((TagNode)author[i]).getText().toString().trim();
            String palyN=((TagNode)playNum[i]).getText().toString().trim();
            String dmNum=((TagNode)danmuNum[i]).getText().toString().trim();
            model.setOid(intOid);
            model.setTitle(((TagNode)title[i]).getText().toString());
            model.setPlayNum(palyN);
            model.setDanmuNum(dmNum);
            model.setAuthor(upper);
            model.setPoint(((TagNode)point[i]).getText().toString());
            model.setRank(intRank);
            model.setDate(new Date());
            model.setVideoUrl(url2);
            model.setImgUrl(img.get(i));
            dataList.add(model);
            //showData(model);
        }
        return dataList;
    }
    public static void showData(VideoModel model){
        System.out.print(model.getOid()+"\t");
        System.out.print(model.getAuthor()+"\t");
        System.out.println(model.getTitle());
        System.out.println(model.getDate());
    }
}
